// @Author EthanScriptOn
// @Desc
package http_operator

import (
	"fmt"
	"gitee.com/fatzeng/srf_switch_basic_components/common/identity"
	"gitee.com/fatzeng/srf_switch_expansion_package/connect"
	"gitee.com/fatzeng/srf_switch_stream_bridge/framework/actuator"
	"io/ioutil"
	"net/http"
)

type HttpOperator struct {
	*identity.IdentityInformationCarrier
}

func (h *HttpOperator) Execute(ctx *actuator.SrfSwitchBridgeActuatorContext, configIdentifier interface{}, structureParam interface{}) (interface{}, error) {
	req, ok := structureParam.(*http.Request)
	if !ok {
		return nil, fmt.Errorf("invalid structureParam type, expected *http.Request")
	}
	client := connect.GetHttpClient()
	resp, err := client.Do(req)
	if err != nil {
		return "", fmt.Errorf("executing request failed: %v", err)
	}
	defer resp.Body.Close()

	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return "", fmt.Errorf("reading response body failed: %v", err)
	}

	return body, nil
}
